#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

bool solve(string A, string B) {
    if (A == B) return true;

    int n = A.size();
    int m = 0;
    while (n--)
    {
        string str1, str2;

        for (int i = 0; i <= m; i++)
        {
            str1 += A[i];
        }
        for (int i = m + 1; i < A.size(); i++)
        {
            str2 += A[i];
        }
        str2 += str1;
        if (str2 == B) { return true; }
        m++;
    }

    return false;
}

int main1()
{
    solve("zanyou", "youzan");
	return 0;
}

class Solution {
public:

    ListNode* head = new ListNode(-1);
    ListNode* mergeKLists(vector<ListNode*>& lists) {

        ListNode* cur = head;
        bool x = true;
        int y = 0;
        while (x)
        {
            int _min = 1001;
            for (int i = 0; i < lists.size(); i++)
            {
                if (lists[i] != nullptr)
                {
                    int _mi = _min;
                    _min = min(_min, lists[i]->val);
                    if (_mi != _min) { y = i; }
                }
            }
            if (_min == 1001) { break; }

            ListNode* next = lists[y]->next;
            cur->next = lists[y];
            cur = cur->next;
            lists[y] = next;
            cur->next = nullptr;
        }

        return head->next;
    }
};

int n, m;
int arr[101][101];
// int memo[101][101];
int dx[4] = { 0,0,1,-1 };
int dy[4] = { 1,-1,0,0 };
int _max = 0;
int k1, k2;
int _long = 1;

void bfs(int x, int y)
{

    vector<pair<int, int>> v;
    for (int k = 0; k < 4; k++)
    {
        int a = x + dx[k], b = y + dy[k];
        if (a >= 0 && a < n && b >= 0 && b < m && arr[a][b] < arr[x][y])
        {
            v.push_back({ a, b });
        }
    }
    if (!v.empty())
    {
        int _max2 = 0;
        pair<int, int> p;
        for (int i = 0; i < v.size(); i++)
        {
            if (arr[v[i].first][v[i].second] > _max2)
            {
                _max2 = arr[v[i].first][v[i].second];
                p.first = v[i].first; p.second = v[i].second;
            }
        }

        _long++;
        bfs(p.first, p.second);
    }
}

int main() {

    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> arr[i][j];
            if (arr[i][j] > _max) { _max = arr[i][j], k1 = i; k2 = j; }
        }
    }
    // memset(memo, 1, sizeof(memo));

    bfs(k1, k2);
    cout << _long << endl;

    return 0;
}

int n, m;
int arr[101][101];
int memo[101][101];
int dx[4] = { 0,0,1,-1 };
int dy[4] = { 1,-1,0,0 };
int _max = 0;
int k1, k2;
int _long;

void bfs(int x, int y)
{

    vector<pair<int, int>> v;
    for (int k = 0; k < 4; k++)
    {
        int a = x + dx[k], b = y + dy[k];
        if (a >= 0 && a < n && b >= 0 && b < m && arr[a][b] < arr[x][y])
        {
            v.push_back({ a, b });
        }
    }
    if (!v.empty())
    {
        int _max2 = 0;
        pair<int, int> p;
        for (int i = 0; i < v.size(); i++)
        {
            if (arr[v[i].first][v[i].second] > _max2)
            {
                _max2 = arr[v[i].first][v[i].second];
                p.first = v[i].first; p.second = v[i].second;
            }
        }

        _long++;
        bfs(p.first, p.second);
    }
}

int main() {

    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> arr[i][j];
        }
    }
    memset(memo, 1, sizeof(memo));

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            _long = 1;
            bfs(i, j);
            _max = max(_max, _long);
        }
    }

    cout << _max << endl;

    return 0;
}